home *** CD-ROM | disk | FTP | other *** search
- /*
- SVEditMain.c
-
- Version 3.0d9
-
- Copyright © SRL Data 1992, 1993
-
- All rights reserved
-
- Produced by : SRL Data
- Originally Developed for UK.DTS
-
- */
- /** This example is brought to you for the purposes of exploration and experimentation of
- System 7.0. It is not intended to form the basis of your own programs- but try out the code-
- that's what it's there for **/
-
- /*System 7.0 specifics-:
-
- We check the configuration on startup to see if we are running on a pre-7.0
- machine. Much as we'd like to handle both environments in 7Edit, we thought
- exiting gracefully under pre-7.0 systems was the best solution.
-
-
- Notice the additions to the main event loop to support section events and the
- despatching of AppleEvents.
-
- New for 3.0d2 :
-
- 21-Feb-92 : NH : Fix type into subscribers
- 26-Feb-92 : NH : ShowBorders grey when no window
- 27-Feb-92 : NH : Test menu and gCurrSection zapped
- 5-Mar-92 : NH : Create Publisher - event driven (ish)
- 27-Mar-92 : NH : Update menu status before MenuKey
-
- Changes for 3.0d5 :
-
- 11-Aug-92 : NH : Quit via AppleEvent
- */
-
- #include <Memory.h>
- #include <Quickdraw.h>
- #include <Types.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Traps.h>
- #include <Packages.h>
- #include <PPCToolBox.h>
- #include <Editions.h>
- #include <Printing.h>
- #include <ToolUtils.h>
- #include <Desk.h>
- #include <Scrap.h>
- #include <OSEvents.h>
- #include <AppleEvents.h>
- #include <AEObjects.h>
- #include <Errors.h>
-
- #include "SVEditGlobals.h"
- #include "SVEditUtils.h"
- #include "SVEditions.h"
- #include "SVAppleEvents.h"
- #include "SVEditWindow.h"
- #include "SVEditFile.h"
-
- /*-----------------------------------------------------------------------*/
- /**---------- Standard Main routines --------------**/
- /*-----------------------------------------------------------------------*/
-
- #pragma segment Main
-
- pascal void MaintainCursor(void)
- {
- Point pt;
- WindowPtr wPtr;
- GrafPtr savePort;
- DPtr theDoc;
-
- wPtr = FrontWindow();
- if (Ours(wPtr))
- {
- theDoc = DPtrFromWindowPtr(wPtr);
- GetPort(&savePort);
- SetPort(wPtr);
- GetMouse(&pt);
- if (theDoc->theText)
- if (PtInRect(pt, &(**(theDoc->theText)).viewRect))
- SetCursor(&editCursor);
- else
- SetCursor(&qd.arrow);
- else
- SetCursor(&qd.arrow);
-
- if (theDoc->theText)
- TEIdle(theDoc->theText);
-
- SetPort(savePort);
- }
- }
-
- #pragma segment Main
-
- pascal void MaintainMenus(void)
- {
- DPtr theDoc;
- WindowPtr firstWindow;
- SectHandle currSection;
-
- firstWindow = FrontWindow();
- if (!Ours(firstWindow))
- {
- EnableItem(myMenus[fileM], fmNew);
- EnableItem(myMenus[fileM], fmOpen);
- DisableItem(myMenus[fileM], fmClose);
- DisableItem(myMenus[fileM], fmSave);
- DisableItem(myMenus[fileM], fmSaveAs);
- DisableItem(myMenus[fileM], fmRevert);
- DisableItem(myMenus[fileM], fmPrint);
- DisableItem(myMenus[fileM], fmPageSetUp);
-
- DisableItem(myMenus[editM], cPublisher);
- DisableItem(myMenus[editM], cSubscriber);
- DisableItem(myMenus[editM], cOptions);
- DisableItem(myMenus[editM], cBorders);
-
- if (firstWindow)
- {
- EnableItem(myMenus[editM], undoCommand);
- EnableItem(myMenus[editM], cutCommand);
- EnableItem(myMenus[editM], copyCommand);
- EnableItem(myMenus[editM], pasteCommand);
- EnableItem(myMenus[editM], clearCommand);
- }
- }
- else
- {
- theDoc = DPtrFromWindowPtr(firstWindow);
- EnableItem(myMenus[editM], pasteCommand);
- EnableItem(myMenus[editM], cBorders);
- EnableItem(myMenus[fileM], fmClose);
- EnableItem(myMenus[fileM], fmSaveAs);
- EnableItem(myMenus[fileM], fmPrint);
- EnableItem(myMenus[fileM], fmPageSetUp);
-
- if (theDoc->dirty)
- EnableItem(myMenus[fileM], fmRevert);
- else
- DisableItem(myMenus[fileM], fmRevert);
-
- if ((theDoc->dirty) && (theDoc->everSaved))
- EnableItem(myMenus[fileM], fmSave);
- else
- DisableItem(myMenus[fileM], fmSave);
-
- DisableItem(myMenus[editM], undoCommand);
-
- if (((**(theDoc->theText)).selEnd - (**(theDoc->theText)).selStart) < 0)
- {
- DisableItem(myMenus[editM], cutCommand);
- DisableItem(myMenus[editM], copyCommand);
- DisableItem(myMenus[editM], clearCommand);
- DisableItem(myMenus[editM], cPublisher);
- }
- else
- {
- EnableItem(myMenus[editM], cutCommand);
- EnableItem(myMenus[editM], copyCommand);
- EnableItem(myMenus[editM], clearCommand);
- EnableItem(myMenus[editM], cPublisher);
- }
-
- currSection = GetSection((**(theDoc->theText)).selStart,
- (**(theDoc->theText)).selEnd,
- theDoc);
- if (currSection)
- {
- DisableItem(myMenus[editM], cPublisher);
- DisableItem(myMenus[editM], cSubscriber);
- EnableItem(myMenus[editM], cOptions);
- if ((**(**currSection).fSectHandle).kind == stPublisher)
- SetItem(myMenus[editM], cOptions, (unsigned char *)"\pPublisher Options…");
- else
- SetItem(myMenus[editM], cOptions, (unsigned char *)"\pSubscriber Options…");
- }
- else
- {
- EnableItem(myMenus[editM], cPublisher);
- EnableItem(myMenus[editM], cSubscriber);
- DisableItem(myMenus[editM], cOptions);
- }
- }
- }
-
- #pragma segment Main
-
- pascal void SetUpCursors(void)
-
- {
- CursHandle hCurs;
-
- hCurs = GetCursor(1);
- editCursor = **hCurs;
- hCurs = GetCursor(watchCursor);
- waitCursor = **hCurs;
- }
-
- #pragma segment Main
-
- pascal void SetUpMenus(void)
- {
- short i;
-
- myMenus[appleM] = GetMenu(appleID);
- AddResMenu(myMenus[appleM], 'DRVR');
- myMenus[fileM] = GetMenu(fileID);
- myMenus[editM] = GetMenu(editID);
- myMenus[fontM] = GetMenu(mfontID);
- AddResMenu(myMenus[fontM], 'FONT');
- myMenus[sizeM] = GetMenu(sizeID);
- myMenus[styleM] = GetMenu(styleID);
-
- for (i = appleM; i <= kLastMenu; i++)
- InsertMenu(myMenus[i], 0);
-
- SetItemStyle(myMenus[styleM], cPlain, 0);
- SetItemStyle(myMenus[styleM], cBold, bold);
- SetItemStyle(myMenus[styleM], cItalic, italic);
- SetItemStyle(myMenus[styleM], cUnderline, underline);
- SetItemStyle(myMenus[styleM], cOutline, outline);
- SetItemStyle(myMenus[styleM], cShadow, shadow);
- SetItemStyle(myMenus[styleM], cCondense, condense);
- SetItemStyle(myMenus[styleM], cExtend, extend);
-
- SetShortMenus(); /* Does a DrawMenuBar() */
- }
-
- pascal void DoFile(short theItem)
- {
- short alertResult;
- DPtr theDoc;
- FSSpec theFSSpec;
- OSErr fileErr;
- TPrint thePSetup;
-
- switch (theItem){
- case fmNew : IssueAENewWindow();
- break;
- case fmOpen: if (GetFile(&theFSSpec)==noErr)
- fileErr = IssueAEOpenDoc(theFSSpec);
- break;
- case fmClose:IssueCloseCommand(FrontWindow());
- break;
- case fmSave:
- case fmSaveAs:
- theDoc = DPtrFromWindowPtr(FrontWindow());
-
- if (theDoc->everSaved == false || theItem==fmSaveAs)
- {
- fileErr = GetFileNameToSaveAs(theDoc);
- if (fileErr!=noErr && fileErr!=userCanceledErr)
- FileError((unsigned char *)"\perror saving ", theDoc->theFileName);
- else
- fileErr = IssueSaveCommand(theDoc->theWindow, &theDoc->theFSSpec);
-
- if (fileErr == noErr)
- SetWTitle(theDoc->theWindow, theDoc->theFSSpec.name);
- }
- else
- fileErr = IssueSaveCommand(theDoc->theWindow, nil);
- break;
-
- case fmRevert:SetCursor(&qd.arrow);
- theDoc = DPtrFromWindowPtr(FrontWindow());
-
- ParamText((unsigned char *)"\pRevert to the last saved version of ", theDoc->theFileName, (unsigned char *)"", (unsigned char *)"");
- alertResult = Alert(AdviseAlert, nil);
- switch (alertResult){
- case aaSave:if (IssueRevertCommand(theDoc->theWindow))
- FileError((unsigned char *)"\perror reverting ", theDoc->theFileName);
- }
- break;
-
- case fmPageSetUp:
- theDoc = DPtrFromWindowPtr(FrontWindow());
- if (DoPageSetup(theDoc))
- {
- thePSetup = **(theDoc->thePrintSetup);
- IssuePageSetupWindow(theDoc->theWindow, thePSetup);
- }
- break;
-
- case fmPrint: IssuePrintWindow(FrontWindow());
- break;
-
- case fmQuit : IssueQuitCommand();
- break;
- } /*of switch*/
- }
-
- #pragma segment Main
-
- pascal void DoCommand(long mResult)
- {
- short theItem;
- short err;
- long result;
- Str255 name;
- DPtr theDocument;
-
- theDocument = DPtrFromWindowPtr(FrontWindow());
-
- theItem = LoWord(mResult);
-
- switch (HiWord(mResult)){
-
- case appleID:
- if (theItem == aboutItem)
- {
- SetCursor(&qd.arrow);
- result = Alert(258, nil);
- }
- else
- {
- GetItem(myMenus[appleM], theItem, name);
- err = OpenDeskAcc(name);
- SetPort(FrontWindow());
- }
- break;
-
- case fileID: DoFile(theItem);
- break;
-
- case editID:
- if (SystemEdit(theItem - 1) == false);
-
- switch (theItem){
-
- case cutCommand : IssueCutCommand(theDocument);
- break;
-
- case copyCommand : IssueCopyCommand(theDocument);
- break;
-
- case pasteCommand : IssuePasteCommand(theDocument);
- break;
-
- case clearCommand : IssueClearCommand(theDocument);
- break;
-
- case selectAllCommand :
- if (theDocument)
- TESetSelect(0,
- (**(theDocument->theText)).teLength,
- theDocument->theText);
- break;
-
- case cPublisher : IssueCreatePublisher(theDocument);
- break;
-
- case cSubscriber: DoSubscribe(theDocument);
- break;
-
- case cOptions : DoSectionOptions(theDocument);
- break;
-
- case cBorders : IssueShowBorders(theDocument->theWindow,
- !theDocument->showBorders);
- break;
-
- } /*of switch*/
- ShowSelect(theDocument);
- break;
-
- case mfontID: IssueFontCommand(theDocument, theItem);
- break;
-
- case sizeID: IssueSizeCommand(theDocument, theItem);
- break;
-
- case styleID: IssueStyleCommand(theDocument, theItem);
- break;
-
- } /*of switch*/
-
- HiliteMenu(0);
-
- }
-
- #pragma segment Main
-
-
- pascal void DoMouseDown(const EventRecord *myEvent)
- {
- WindowPtr whichWindow;
- Point p;
- Rect dragRect;
- DPtr theDoc;
-
- p = myEvent->where;
- switch (FindWindow(p, &whichWindow)){
-
- case inDesk: SysBeep(10);
- break;
-
- case inGoAway:if (Ours(whichWindow))
- if (TrackGoAway(whichWindow, p))
- IssueCloseCommand(whichWindow);
- break;
- case inMenuBar:
- SetCursor(&qd.arrow);
- theDoc = DPtrFromWindowPtr(FrontWindow());
- if (theDoc)
- {
- SetFontMenu(theDoc);
- SetEditMenu(theDoc);
- }
-
- DoCommand(MenuSelect(p));
- HiliteMenu(0);
- break;
-
- case inSysWindow: SystemClick(myEvent, whichWindow);
- break;
-
- case inDrag:
-
- dragRect = qd.screenBits.bounds;
-
- if (Ours(whichWindow))
- {
- DragWindow(whichWindow, p, &dragRect);
- /*
- As rgnBBox may be passed by address
- */
- dragRect = (**((WindowPeek)whichWindow)->strucRgn).rgnBBox;
- /*
- The windows already there, but still tell
- the our AppleEvents core about the move in case
- they want to do anything
- */
- IssueMoveWindow(whichWindow, dragRect);
- }
- break;
-
- case inGrow:SetCursor(&qd.arrow);
- if (Ours(whichWindow))
- MyGrowWindow(whichWindow, p);
- break;
-
- case inZoomIn : DoZoom(whichWindow, inZoomIn, p);
- break;
-
- case inZoomOut: DoZoom(whichWindow, inZoomOut, p);
- break;
- case inContent: if (whichWindow != FrontWindow())
- SelectWindow(whichWindow);
- else
- if (Ours(whichWindow))
- DoContent(whichWindow, *myEvent);
- break;
- } /*of switch*/
- }
-
- #pragma segment Main
-
- pascal long GetSleep(void)
- {
- long sleep;
- WindowPtr theWindow;
- DPtr theDoc;
-
- sleep = 0x7fffffff;
- if (!gInBackground)
- {
- theWindow = FrontWindow();
- if (theWindow)
- {
- theDoc = DPtrFromWindowPtr(theWindow);
- if ((**(theDoc->theText)).selStart == (**(theDoc->theText)).selEnd)
- sleep = GetCaretTime();
- }
- }
- return(sleep);
- } /*GetSleep*/
-
- #pragma segment Main
-
-
- pascal void MainEvent(void)
- {
- DPtr theDoc;
- EventRecord myEvent;
- char theChar;
- WindowPtr theWindow;
- Boolean activate;
- Boolean keyIsOk;
- SectHandle currSection;
-
-
- MaintainCursor(); /* TEIdle in here for now */
- MaintainMenus();
-
- if (WaitNextEvent(everyEvent, &myEvent, GetSleep(), nil))
- {
- switch (myEvent.what) {
- case mouseDown: FlushAndRecordTypingBuffer();
- DoMouseDown(&myEvent);
- break;
- case keyDown:
- case autoKey:
- theDoc = DPtrFromWindowPtr(FrontWindow());
-
- theChar = myEvent.message & charCodeMask;
-
- if ((myEvent.modifiers & cmdKey) == cmdKey)
- {
- DoCommand(MenuKey(theChar));
- HiliteMenu(0);
- }
- else
- if (theDoc->theText)
- {
- keyIsOk = true;
- /*
- don't allow a subscriber to be changed
- */
- currSection = GetSection((**(theDoc->theText)).selStart,
- (**(theDoc->theText)).selEnd,
- theDoc);
-
- if (currSection)
- if ((**(**currSection).fSectHandle).kind == stSubscriber)
- keyIsOk = KeyOKinSubscriber(theChar);
-
- if (keyIsOk)
- {
- DoTEKeySectionRecalc(theDoc, theChar);
-
- AddKeyToTypingBuffer(theDoc, theChar);
-
- TEKey(theChar, theDoc->theText);
- AdjustScrollbars(theDoc, false);
-
- ShowSelect(theDoc);
-
- theDoc->dirty = true;
- }
- }
- break;
-
- case activateEvt:
- activate = ((myEvent.modifiers & activeFlag) != 0);
- theWindow = (WindowPtr)myEvent.message;
- DoActivate(theWindow, activate);
- break;
-
- case updateEvt:
- theDoc = DPtrFromWindowPtr((WindowPtr)myEvent.message);
- DoUpdate(theDoc);
- break;
-
- case kHighLevelEvent: FlushAndRecordTypingBuffer();
- DoAppleEvent(myEvent);
- break;
- case kOSEvent:
-
- switch (myEvent.message & osEvtMessageMask) { /*high byte of message*/
- case 0x01000000:
- {
- gInBackground = ((myEvent.message & resumeFlag) == 0);
- DoActivate(FrontWindow(), !gInBackground);
- }
- }
- }
- } /*of switch*/
- }
-
- #pragma segment Main
-
- pascal void DoSVEdit(void)
- {
- OSErr err;
- short result;
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- MaxApplZone();
- SetUpCursors();
-
- SetUpMenus();
-
- gWCount = 0;
- gNewDocCount = 0;
- gQuitting = false;
- gFontMItem = 0;
-
- gGestaltAvailable = false;
- gAppleEventsImplemented = false;
- gAliasManagerImplemented = false;
- gEditionManagerImplemented = false;
- gOutlineFontsImplemented = false;
-
- /*check environment checks to see if we are running 7.0*/
- if (!CheckEnvironment())
- {
- SetCursor(&qd.arrow);
- /*pose the only 7.0 alert*/
- result = Alert(302, nil);
- return;
- }
-
- err = InitEditionPack();
-
- if (err)
- {
- ShowError((unsigned char *)"\pInitEditionPack", err);
- gQuitting = true;
- }
-
- err = AEObjectInit();
- if (err)
- {
- ShowError((unsigned char *)"\pAEObjectInit", err);
- gQuitting = true;
- }
-
- InitAppleEvents();
-
- err = PPCInit();
- if (err)
- {
- ShowError((unsigned char *)"\pPPCInit", err);
- gQuitting = true;
- }
-
- while (!gQuitting)
- MainEvent();
- }
-
- main ()
- {
- /*the main routine starts here*/
- DoSVEdit();
- }
-